﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Input;
using JPR.TunelProxy.RequestGenerator.Helpers;
using JPR.TunelProxy.RequestGenerator.Model;

namespace JPR.TunelProxy.RequestGenerator.ViewModel
{
	/// <summary>
	/// 
	/// </summary>
	internal class MainWindowViewModel : ViewModelBase
	{
		#region Private properties

		private readonly MainWindowModel _model;

		#endregion

		#region Handlers

		/// <summary>
		/// When this event is raised, the main window will be closed. See App.xaml.cs.
		/// </summary>
		internal event EventHandler RequestClose;

		#endregion

		#region Commands

		/// <summary>
		/// Command for closing the application. Bind this to an 'exit' UI input.
		/// </summary>
		public ICommand CloseCommand { get; private set; }

		public ICommand CreateRequestCommand { get; private set; }

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		internal MainWindowViewModel()
		{
			_model = new MainWindowModel();
			foreach (HttpCommand value in Enum.GetValues(typeof (HttpCommand)))
			{
				_model.CommandList.Add(value);
			}

			//The predicate argument can be used, for example, to show a close confirmation dialog.
			CloseCommand = new RelayCommand(o => OnRequestClose(), o => true);

			CreateRequestCommand = new RelayCommand(o => OnRequestCreateRequest(), o => true);

			Address = "http://www.google.com/";
		}

		#endregion

		#region Command bonding

		private void OnRequestClose()
		{
			if (RequestClose != null)
			{
				RequestClose(this, new EventArgs());
			}
		}

		private void OnRequestCreateRequest()
		{
			//if(!IsValid)
			//{
			//    NotifyAboutErrors();
			//}
			Regex regex = new Regex(@"^(https?://)?(?<address>[^:/]+):?(?<port>\d*)(?<objectName>/?.*)$", RegexOptions.IgnoreCase);
			Match match = regex.Match(_model.Address.Replace('\\', '/'));
			RequestHelper.SendRequest(_model.Command, match.Groups["address"].Value, match.Groups["objectName"].Value,
			                            int.Parse(string.IsNullOrWhiteSpace(match.Groups["port"].Value)
			                                      	? "80"
			                                      	: match.Groups["port"].Value));
		}

		#endregion

		#region Public properties

		public HttpCommand Command
		{
			get { return _model.Command; }
			set
			{
				if (value != _model.Command)
				{
					_model.Command = value;
					OnPropertyChanged("Command");
				}
			}
		}

		public HttpCommand[] CommandList
		{
			get { return _model.CommandList.ToArray(); }
		}

		public string Address
		{
			get { return _model.Address; }
			set
			{
				if (value != _model.Address)
				{
					_model.Address = value;
					OnPropertyChanged("Address");
				}
			}
		}

		public IDictionary<string, string> Headers
		{
			get { return _model.Headers; }
			set
			{
				_model.Headers.Clear();
				foreach (KeyValuePair<string, string> pair in value)
				{
					_model.Headers.Add(pair);
				}
				OnPropertyChanged("Headers");
			}
		}

		#endregion
	}
}